<?php

namespace App\Http\Controllers\Admin;

use App\Models\Activity;
use Illuminate\Http\Request;

class ActivityController extends BaseController
{
    public function __construct()
    {
        parent::__construct();
        $this->middleware('check_admin_login');
    }

    public function index(Request $request)
    {
        $page      = intval($request->get('page',1));
        $page      = max($page ,1);
        $page_size = intval($request->get('page_size',10));

        $builder = Activity::query()->where('deleted',0);

        $columns = ['id','title','image','url','position','created_at'];
        $list = $builder->orderBy('id','desc')->paginate($page_size,$columns,'page',$page);

        $items = [];
        if(!empty($list->items()))
        {
            foreach ($list->items() as $item)
            {
                $items[] = [
                    'id'         => $item['id'],
                    'image'      => $item['image'],
                    'title'      => $item['title'],
                    'url'        => $item['url'],
                    'position'   => $item['position'],
                    'created_at' => $item['created_at'],
                ];
            }
        }

        $data = [
            '_meta' => [
                'total_count'  => $list->total(),
                'page_count'   => $list->lastPage(),
                'current_page' => $list->currentPage(),
                'per_page'     => $list->perPage()
            ],
            "items" => $items
        ];

        return $this->res([
            'data' => $data
        ]);
    }

    public function store(Request $request)
    {
        $tilte    = trim($request->get('title'));
        $url      = trim($request->get('url'));
        $image    = trim($request->get('image'));
        $position = intval($request->get('position',1));

        if(empty($tilte) || empty($url) || empty($image) || empty($position))
        {
            return $this->err(1,200,'','参数错误');
        }

        $activity = Activity::create([
            'title'       => $tilte,
            'url'         => $url,
            'image'       => $image,
            'position'    => $position,
            'created_at'  => $this->time,
            'updated_at'  => $this->time
        ]);
        //dd($activity);
        return $this->ok([],'添加成功');
    }

    public function update(Request $request)
    {
        $id       = intval($request->get('id'));
        $tilte    = trim($request->get('title'));
        $url      = trim($request->get('url'));
        $image    = trim($request->get('image'));
        $position = intval($request->get('position',1));

        if(empty($id) || empty($tilte) || empty($url) || empty($image) || empty($position))
        {
            return $this->err(1,200,'','参数错误');
        }

        $activity = $this->_check_info($id);
        $activity->update([
            'title'       => $tilte,
            'url'         => $url,
            'image'       => $image,
            'position'    => $position,
            'updated_at'  => $this->time
        ]);
        return $this->ok([],'修改成功');
    }

    public function show(Request $request)
    {
        $id = intval($request->get('id'));
        if(empty($id))
        {
            return $this->err(1,200,'','参数错误');
        }

        $activity = $this->_check_info($id);
        if(empty($activity))
        {
            return $this->err(1,200,'','活动不存在');
        }

        $result = [
            'id' => $activity->id,
            'title' => $activity->title,
            'url' => $activity->url,
            'image' => $activity->image,
        ];
        return $this->ok($result);
    }

    public function destroy(Request $request)
    {
        $id = intval($request->get('id'));
        if(empty($id))
        {
            return $this->err(1,200,'','参数错误');
        }

        $activity = $this->_check_info($id);
        if(empty($activity))
        {
            return $this->err(1,200,'','活动不存在');
        }
        $activity->update([
            'deleted'     => 1,
            'deleted_at'  => $this->time
        ]);
        return $this->ok([],'删除成功');
    }

    private function _check_info($id)
    {
        $activity = Activity::query()->where('id',$id)->where('deleted',0)->first();
        return $activity;
    }

}
